<template>
	<view class="containar">
		<view class="avatarUrl">
			<text><text style="color: red;">*</text>头像：</text>
			<button type="balanced" class="upload-btn" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
				<image :src="avatarUrl" class="refreshIcon"></image>
			</button>
		</view>
		<view class="nickname">
			<text><text style="color: red;">*</text>昵称：</text>
			<input type="nickname" class="weui-input" :value="nickName" @blur="bindblur" placeholder="请输入昵称"
				@input="bindinput" />
		</view>

		<view class="btn">
			<view class="btn-sub" @click="onSubmit">保存</view>
		</view>
	</view>
</template>


<script>
	import {
		uploadFile
	} from '../../api/active';
	export default {
		data() {
			return {
				avatarUrl: '',
				nickName: ''
			};
		},
		onLoad(option) {},
		methods: {
			bindblur(e) {
				this.nickName = e.detail.value; // 获取微信昵称
			},
			bindinput(e) {
				this.nickName = e.detail.value; //这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮，会出现修改不成功的情况。
			},
			onChooseavatar(e) {
				let self = this;
				let {
					avatarUrl
				} = e.detail;
				uni.showLoading({
					title: '加载中'
				});
			uploadFile({
				filename: 'avatar' + new Date().getTime() + '.png'
			}).then(res => {
				const aliOss = res.data
				const aliOssUrl = aliOss.host;
				delete aliOss.host;
				const formData = [];
				for (const key in aliOss) {
					if (aliOss.hasOwnProperty(key)) {
						formData.push({
							name: key,
							value: aliOss[key]
						});
					}
				}
				// console.log(Object.fromEntries(formData.map(item => [item.name, item.value])))
				uni.uploadFile({
					url: aliOssUrl,
					filePath: avatarUrl, // Use the file's path in Uni-app
					name: "file",
					formData: Object.fromEntries(formData.map(item => [item.name, item.value])),
					success: (uploadFileRes) => {
						self.avatarUrl = aliOssUrl + '/' + aliOss.key
						uni.hideLoading();
					},
					fail: (err) => {
						console.log(err)
						uni.hideLoading();
					}
				});
			})
			},
			onSubmit() {
				if (!this.nickName || !this.avatarUrl) {
					uni.showToast({
						icon: 'none',
						title: '请将信息填写完整'
					})
					return false;
				}else {
					uni.redirectTo({
						url: `../login/login?name=${this.nickName}&avatarUrl=${this.avatarUrl}`
					})
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.containar {
		.avatarUrl {
			padding: 80rpx 0 40rpx 40rpx;
			background: #fff;
			display: flex;
			align-items: center;
			button {
				background: #fff;
				padding: 0;
				margin: 0;
				display: flex;
				justify-content: center;
				align-items: center;
				width: 160rpx;
				height: 160rpx;
				box-sizing: border-box;
				margin-left: 60rpx;
				.refreshIcon {
					width: 100%;
					height: 100%;
					// border-radius: 50%;
				}

				.jt {
					width: 14rpx;
					height: 28rpx;
				}
			}
		}

		.nickname {
			background: #fff;
			padding: 20rpx 30rpx 0 40rpx;
			display: flex;
			align-items: center;
			// justify-content: center;

			.weui-input {
				padding-left: 60rpx;
			}
		}

		.btn {
			width: 100%;

			.btn-sub {
				width: 670rpx;
				margin: 40rpx auto 0;
				height: 90rpx;
				background: #005097;
				border-radius: 45rpx;
				line-height: 90rpx;
				text-align: center;
				font-size: 36rpx;
				color: #fff;
			}
		}
	}
</style>